﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class CallListControl : RSBaseUserControl
    {
        int _companyid = 0;
        int _locationid = 0;

        
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void gvCall_Select(Object sender, CommandEventArgs e)
        {
            int callid = Convert.ToInt32(e.CommandArgument.ToString());
            Response.Redirect("RSCallDetail.aspx?callid=" + callid);
        }
        protected void btnAddCallToLocation_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                _companyid = userIdentity.RSCompanyId;
                if (!String.IsNullOrEmpty(hfSelectedLocationId.Value))
                    _locationid = Convert.ToInt32(hfSelectedLocationId.Value);

                if (_companyid > 0 && _locationid > 0)
                {
                    ucModalCallDetailControl.ShowModal(_companyid, _locationid, 0);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);

        }

        public void LoadCallDataByLocationID(int companyid, int locationid)
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            if (companyid > 0 && locationid > 0)
            {
                hfSelectedLocationId.Value = locationid.ToString();
                String sqlSELECT = "select CallId, CallDt, Subject, Description, Response, CompanyId, CallTypeId, CallType, City, State, Location, LocationId, Zip, StatusId, Status from rs_vwCall where locationid=@locationid and companyid=@companyid order by calldt desc";

                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sqlSELECT, myConnection);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@companyid";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@locationid";
                param.Value = locationid;
                myCommand.Parameters.Add(param);

                SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
                myConnection.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                gvCallListGrid.DataSource = ds;
                gvCallListGrid.DataBind();
                myConnection.Close();

            }
        }

        protected void gvCallListGrid_Delete(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int callid = Convert.ToInt32(e.CommandArgument.ToString());
                if (DeleteServiceCall(companyid, callid))
                {
                    ucModalActionMessageControl.ShowModal("Service Call deleted succesfully!", Request.RawUrl, true);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        private bool DeleteServiceCall(int companyid, int callid)
        {
            bool ret = false;
            if (companyid > 0 && callid> 0)
            {
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@callid", SqlDbType.Int, 0, ParameterDirection.Input, callid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblCallLocation WHERE callid=@callid AND companyid=@companyid; DELETE FROM rs_tblCall WHERE callid=@callid AND  CompanyId=@companyid");
                SQLHelper.ExecuteScalarCmd(sqlCmd);
                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                ret = true;
            }
            return ret;
        }
    }

    
}